package com.restaurant.application.indexes.exchangegoodslist.mysql;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.nebutown.base.Unit;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.nebutown.supporthome.AbstractIndex;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

public class ExchangeGoodsListImpl extends AbstractIndex<ExchangeGoodsListCriteria, ExchangeGoodsListBean> {

    @Override
    public String getTableName() {
        return "" + getConnectionName() + ".EXCHANGEGOODSLIST";
    }

    @Override
    public ExchangeGoodsListBean newBean() {
        return new ExchangeGoodsListBean();
    }

    @Override
    public ExchangeGoodsListCriteria newCriteria() {
        return new ExchangeGoodsListCriteria();
    }

    @Override
    public List<String> getFieldNames() {
        return Lists.newArrayList(
                "ExchangeGoods",
                "accumulate",
                "createTime",
                "customerUid",
                "customerId",
                "phone",
                "nickName",
                "goodsUID",
                "goodsName",
                "goodsId");
    }

    @Override
    public List<String> getDateFields() {
        return Lists.newArrayList(
                "accumulate",
                "createTime");
    }

    @Override
    public List<String> getDoubleFields() {
        return Lists.newArrayList(
                "");
    }

    @Override
    public Map<String, String> getFieldLabels() {
        return ImmutableMap.<String, String>builder()
                .put("accumulate", "兑换积分")
                .put("createTime", "创建时间")
                .put("customerId", "客户名")
                .put("phone", "客户电话")
                .put("nickName", "客户昵称")
                .put("goodsName", "商品名称")
                .put("goodsId", "商品Id")
                .build();
    }

    @Override
    public List<String> getContributors() {
        return Lists.newArrayList(
                "com.restaurant.application.exchangegoods.ExchangeGoods",
                "com.restaurant.application.genshanguser.GenShangUser",
                 "com.restaurant.application.accumulategoods.AccumulateGoods");
    }

    @Override
    public ExchangeGoodsListBean fakeBean() {
        ExchangeGoodsListBean bean = new ExchangeGoodsListBean();

        bean.setExchangeGoods(UID.recover("A -177646115910768819 looooong string"));
        bean.setAccumulate(1099095506L);
        bean.setCreateTime(-90186384L);
        bean.setCustomerUid(UID.recover("A 3406145529278553823 looooong string"));
        bean.setCustomerId("A 5216778090475874810 looooong string");
        bean.setPhone("A -7737047310712519103 looooong string");
        bean.setNickName("A 8656505953568688103 looooong string");
        bean.setGoodsUID(UID.recover("A -5277107645494389560 looooong string"));
        bean.setGoodsName("A 6828405254894917539 looooong string");
        bean.setGoodsId("A -2085998303391268199 looooong string");
        return bean;
    }

    @Override
    public String getCreateTableStatement() {
        StringBuilder buf = new StringBuilder();

        buf.append("CREATE TABLE IF NOT EXISTS " + getConnectionName() + ".EXCHANGEGOODSLIST (\n");
        buf.append("    RECORD_ID BIGINT AUTO_INCREMENT,\n");
        buf.append("    EXCHANGEGOODS CHAR(128) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,\n");
        buf.append("    ACCUMULATE BIGINT,\n");
        buf.append("    CREATETIME BIGINT,\n");
        buf.append("    CUSTOMERUID CHAR(128) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,\n");
        buf.append("    CUSTOMERID VARCHAR(255),\n");
        buf.append("    PHONE VARCHAR(255),\n");
        buf.append("    NICKNAME VARCHAR(255),\n");
        buf.append("    GOODSUID CHAR(128) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,\n");
        buf.append("    GOODSNAME VARCHAR(255),\n");
        buf.append("    GOODSID VARCHAR(255),\n");
        buf.append("PRIMARY KEY (RECORD_ID)) CHARACTER SET  = utf8mb4 COLLATE = utf8mb4_bin");
        return buf.toString();
    }

    @Override
    public String getInsertDataStatement(ExchangeGoodsListBean data) {
        StringBuilder buf = new StringBuilder();

        buf.append("INSERT INTO " + getConnectionName() + ".EXCHANGEGOODSLIST (\n    ");
        buf.append("    EXCHANGEGOODS, ACCUMULATE, CREATETIME, CUSTOMERUID, CUSTOMERID, PHONE, NICKNAME, GOODSUID, GOODSNAME, GOODSID");
        buf.append(") VALUES (");
        buf.append("    '" + cut(data.getExchangeGoods(), 128) + "', " + data.getAccumulate() + ", " + data.getCreateTime() + ", '" + cut(data.getCustomerUid(), 128) + "', '" + cut(data.getCustomerId(), 255) + "', '" + cut(data.getPhone(), 255) + "', '" + cut(data.getNickName(), 255) + "', '" + cut(data.getGoodsUID(), 128) + "', '" + cut(data.getGoodsName(), 255) + "', '" + cut(data.getGoodsId(), 255) + "'");
        buf.append(")");
        return buf.toString();
    }

    @Override
    public String getDeleteRowsStatement(Long rowId) {
        return "DELETE FROM " + getConnectionName() + ".EXCHANGEGOODSLIST WHERE  RECORD_ID = " + rowId;
    }

    @Override
    public String getLocalUpdateStatement(ExchangeGoodsListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".EXCHANGEGOODSLIST \n");
        buf.append("   SET ");
        if(data.getExchangeGoods() != null) {
            buf.append(sep.toString() + "\n      EXCHANGEGOODS = '" + cut(data.getExchangeGoods(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getAccumulate() != null) {
            buf.append(sep.toString() + "\n      ACCUMULATE = " + data.getAccumulate() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCustomerUid() != null) {
            buf.append(sep.toString() + "\n      CUSTOMERUID = '" + cut(data.getCustomerUid(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCustomerId() != null) {
            buf.append(sep.toString() + "\n      CUSTOMERID = '" + cut(data.getCustomerId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getPhone() != null) {
            buf.append(sep.toString() + "\n      PHONE = '" + cut(data.getPhone(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getNickName() != null) {
            buf.append(sep.toString() + "\n      NICKNAME = '" + cut(data.getNickName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsUID() != null) {
            buf.append(sep.toString() + "\n      GOODSUID = '" + cut(data.getGoodsUID(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsName() != null) {
            buf.append(sep.toString() + "\n      GOODSNAME = '" + cut(data.getGoodsName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsId() != null) {
            buf.append(sep.toString() + "\n      GOODSID = '" + cut(data.getGoodsId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(!hasData) return "";

        hasData = false;
        buf.append(" WHERE TRUE\n");
        if(data.getExchangeGoods() != null) {
            buf.append("    AND EXCHANGEGOODS = '" + data.getExchangeGoods() + "'\n");
            hasData = true;
        }
        if(data.getCustomerUid() != null) {
            buf.append("    AND CUSTOMERUID = '" + data.getCustomerUid() + "'\n");
            hasData = true;
        }
        if(data.getGoodsUID() != null) {
            buf.append("    AND GOODSUID = '" + data.getGoodsUID() + "'\n");
            hasData = true;
        }
        if(!hasData) return "";

        return buf.toString();
    }

    @Override
    public String getUpdateByIdStatement(ExchangeGoodsListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".EXCHANGEGOODSLIST \n");
        buf.append("   SET ");
        if(data.getExchangeGoods() != null) {
            buf.append(sep.toString() + "\n      EXCHANGEGOODS = '" + cut(data.getExchangeGoods(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getAccumulate() != null) {
            buf.append(sep.toString() + "\n      ACCUMULATE = " + data.getAccumulate() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCustomerUid() != null) {
            buf.append(sep.toString() + "\n      CUSTOMERUID = '" + cut(data.getCustomerUid(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCustomerId() != null) {
            buf.append(sep.toString() + "\n      CUSTOMERID = '" + cut(data.getCustomerId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getPhone() != null) {
            buf.append(sep.toString() + "\n      PHONE = '" + cut(data.getPhone(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getNickName() != null) {
            buf.append(sep.toString() + "\n      NICKNAME = '" + cut(data.getNickName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsUID() != null) {
            buf.append(sep.toString() + "\n      GOODSUID = '" + cut(data.getGoodsUID(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsName() != null) {
            buf.append(sep.toString() + "\n      GOODSNAME = '" + cut(data.getGoodsName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsId() != null) {
            buf.append(sep.toString() + "\n      GOODSID = '" + cut(data.getGoodsId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(!hasData) return "";

        hasData = false;
        buf.append(" WHERE RECORD_ID = " + data.getRecordId());
        return buf.toString();
    }

    @Override
    public ExchangeGoodsListBean extractLine(ResultSet resultSet) throws Exception {
        ExchangeGoodsListBean result = new ExchangeGoodsListBean();

        result.setRecordId(resultSet.getLong("RECORD_ID"));
        result.setExchangeGoods(UID.recover(resultSet.getString("ExchangeGoods")));
        result.setAccumulate(resultSet.getLong("accumulate"));
        result.setCreateTime(resultSet.getLong("createTime"));
        result.setCustomerUid(UID.recover(resultSet.getString("customerUid")));
        result.setCustomerId(resultSet.getString("customerId"));
        result.setPhone(resultSet.getString("phone"));
        result.setNickName(resultSet.getString("nickName"));
        result.setGoodsUID(UID.recover(resultSet.getString("goodsUID")));
        result.setGoodsName(resultSet.getString("goodsName"));
        result.setGoodsId(resultSet.getString("goodsId"));
        return result;
    }

    @Override
    public String getWherePart(ExchangeGoodsListCriteria criteria, SortSpec sort) {
        StringBuilder buf = new StringBuilder(" TRUE ");

        criteria.setCustomerIdLike(searchString(criteria.getCustomerIdLike()));
        criteria.setPhoneLike(searchString(criteria.getPhoneLike()));
        criteria.setNickNameLike(searchString(criteria.getNickNameLike()));
        criteria.setGoodsNameLike(searchString(criteria.getGoodsNameLike()));
        criteria.setGoodsIdLike(searchString(criteria.getGoodsIdLike()));

        if(criteria.getRecordId() != null) {
            buf.append("    AND RECORD_ID = " + criteria.getRecordId());
        }
        if(criteria.getExchangeGoodsEquals() != null) {
            buf.append("    AND EXCHANGEGOODS = '" + criteria.getExchangeGoodsEquals() + "'");
        }
        if(criteria.getExchangeGoodsNotEquals() != null) {
            buf.append("    AND EXCHANGEGOODS != '" + criteria.getExchangeGoodsNotEquals() + "'");
        }
        if(criteria.getAccumulateUpper() != null) {
            buf.append("    AND ACCUMULATE <= " + criteria.getAccumulateUpper());
        }
        if(criteria.getAccumulateLower() != null) {
            buf.append("    AND ACCUMULATE >= " + criteria.getAccumulateLower());
        }
        if(criteria.getAccumulateEquals() != null) {
            buf.append("    AND ACCUMULATE = '" + criteria.getAccumulateEquals() + "'");
        }
        if(criteria.getAccumulateNotEquals() != null) {
            buf.append("    AND ACCUMULATE != '" + criteria.getAccumulateNotEquals() + "'");
        }
        if(criteria.getCreateTimeUpper() != null) {
            buf.append("    AND CREATETIME <= " + criteria.getCreateTimeUpper());
        }
        if(criteria.getCreateTimeLower() != null) {
            buf.append("    AND CREATETIME >= " + criteria.getCreateTimeLower());
        }
        if(criteria.getCreateTimeEquals() != null) {
            buf.append("    AND CREATETIME = '" + criteria.getCreateTimeEquals() + "'");
        }
        if(criteria.getCreateTimeNotEquals() != null) {
            buf.append("    AND CREATETIME != '" + criteria.getCreateTimeNotEquals() + "'");
        }
        if(criteria.getCustomerUidEquals() != null) {
            buf.append("    AND CUSTOMERUID = '" + criteria.getCustomerUidEquals() + "'");
        }
        if(criteria.getCustomerUidNotEquals() != null) {
            buf.append("    AND CUSTOMERUID != '" + criteria.getCustomerUidNotEquals() + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getCustomerIdEquals())) {
            buf.append("    AND CUSTOMERID = '" + cut(criteria.getCustomerIdEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getCustomerIdNotEquals())) {
            buf.append("    AND CUSTOMERID != '" + cut(criteria.getCustomerIdNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getCustomerIdLike())) {
            buf.append("    AND CUSTOMERID LIKE '" + cut(criteria.getCustomerIdLike()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getPhoneEquals())) {
            buf.append("    AND PHONE = '" + cut(criteria.getPhoneEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getPhoneNotEquals())) {
            buf.append("    AND PHONE != '" + cut(criteria.getPhoneNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getPhoneLike())) {
            buf.append("    AND PHONE LIKE '" + cut(criteria.getPhoneLike()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getNickNameEquals())) {
            buf.append("    AND NICKNAME = '" + cut(criteria.getNickNameEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getNickNameNotEquals())) {
            buf.append("    AND NICKNAME != '" + cut(criteria.getNickNameNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getNickNameLike())) {
            buf.append("    AND NICKNAME LIKE '" + cut(criteria.getNickNameLike()) + "'");
        }
        if(criteria.getGoodsUIDEquals() != null) {
            buf.append("    AND GOODSUID = '" + criteria.getGoodsUIDEquals() + "'");
        }
        if(criteria.getGoodsUIDNotEquals() != null) {
            buf.append("    AND GOODSUID != '" + criteria.getGoodsUIDNotEquals() + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsNameEquals())) {
            buf.append("    AND GOODSNAME = '" + cut(criteria.getGoodsNameEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsNameNotEquals())) {
            buf.append("    AND GOODSNAME != '" + cut(criteria.getGoodsNameNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsNameLike())) {
            buf.append("    AND GOODSNAME LIKE '" + cut(criteria.getGoodsNameLike()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsIdEquals())) {
            buf.append("    AND GOODSID = '" + cut(criteria.getGoodsIdEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsIdNotEquals())) {
            buf.append("    AND GOODSID != '" + cut(criteria.getGoodsIdNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsIdLike())) {
            buf.append("    AND GOODSID LIKE '" + cut(criteria.getGoodsIdLike()) + "'");
        }
        if(sort != null && sort.getColumns().size() > 0) {
            buf.append(" ORDER BY ");
            sort.getColumns().forEach(c->buf.append(c.getColumnName().toUpperCase())
                .append(" ").append((c.getAscending() != null && c.getAscending()) ? "ASC" : "DESC")
                .append(", "));
            buf.append("RECORD_ID ASC");
        }
        return buf.toString();
    }

    @Override
    public <T extends Unit > String getUpdateStatement(T source) {
        ExchangeGoodsListBean data = null;

        if(source instanceof com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListFromExchangeGoodsIF) {
            data = new ExchangeGoodsListBean();
             ((com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListFromExchangeGoodsIF)source).getExchangeGoodsDataForExchangeGoodsList(data);
        }
        if(source instanceof com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListFromcustomerUidIF) {
            data = new ExchangeGoodsListBean();
             ((com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListFromcustomerUidIF)source).getCustomerUidDataForExchangeGoodsList(data);
        }
        if(source instanceof com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListFromgoodsUIDIF) {
            data = new ExchangeGoodsListBean();
             ((com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListFromgoodsUIDIF)source).getGoodsUIDDataForExchangeGoodsList(data);
        }

        return data == null ? null: getLocalUpdateStatement(data);
    }
}
